package com.wildwolf.project.wildwolf.ui;



import com.wildwolf.project.wildwolf.R;
import com.wildwolf.project.wildwolf.WildWolfApplication;
import com.wildwolf.project.wildwolf.config.AppConfig;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.InputFilter;
import android.text.LoginFilter;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity  implements View.OnClickListener{
	
	

    private static final int MANUAL_CONFIGURATION = 1;
    private EditText accountOpinion;
    private EditText accountPassword;
//    private final OpinionTextWatcher opinionTextWatcher = new OpinionTextWatcher();
// private final PasswordTextWatcher passwordTextWatcher = new PasswordTextWatcher();

	private TextView textViewCheckOk;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setProgressBarIndeterminateVisibility(true);  
		setContentView(R.layout.layout_login);
		accountOpinion = (EditText)findViewById(R.id.opinionId);
		accountPassword= (EditText)findViewById(R.id.passWord);
		textViewCheckOk = (TextView) findViewById(R.id.checkOk);	
		textViewCheckOk.setOnClickListener(this);
		InputFilter[] orgFilters = accountOpinion.getFilters();
		InputFilter[] newFilters = new InputFilter[orgFilters.length + 1];
		int i;
		for (i = 0; i < orgFilters.length; i++)
		    newFilters[i] = orgFilters[i];
		newFilters[i] = new LoginFilter.UsernameFilterGeneric();
		accountOpinion.setFilters(newFilters);

	}

	@Override
	public void onClick(View v) {
		Intent i = null;
		if (v == textViewCheckOk) {
		    configureAccount();
		    i = new Intent(this, Login.class);
		    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		    startActivity(i);
		    finish();
		} else {
		    i = new Intent(this, Settings.class);
		    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		    startActivityForResult(i, MANUAL_CONFIGURATION);
		}
	}
	
    /**
     * 存储用户名和密码
     */
    private void configureAccount() {
	SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
	SharedPreferences.Editor edit = settings.edit();
	edit.putString(WildWolfApplication.ACCOUNT_USERNAME_KEY, accountOpinion.getText().toString()+"@"+AppConfig.XMPP_SERVER);
	edit.putString(WildWolfApplication.ACCOUNT_PASSWORD_KEY, accountPassword.getText().toString());
	edit.commit();
    }
	/**
	 * 鐢ㄦ埛鍚嶇洃瑙嗗櫒
	 * @author LEON
	 *
	 */
class OpinionTextWatcher implements TextWatcher{

	@Override
	public void afterTextChanged(Editable s) {
		
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		
	}
	
	
}
	/**
	 *  瀵嗙爜鐩戣鍣�
	 * @author LEON
	 *
	 */
class PasswordTextWatcher implements TextWatcher {

	@Override
	public void afterTextChanged(Editable s) {
		
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		
	}
	
}
}
